﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_EditCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string category_ID = Request.QueryString["Category_ID"];

           
                LoadControlForEdit(category_ID);
        }
        else
        {

        }
    }
    #region For Set control when edit/insert
    private void SetControlForInsert()
    {
       // btnAdd.Visible = true;
       // btnEdit.Visible = false;
    }

    private void LoadControlForEdit(string category_ID)
    {
        Category category = Category_Session.Get_Category_ByID(int.Parse(category_ID));
        if (category != null)
        {
            //Gan control voi cac truong trong doi tuong
            txtCateLink.Value = category.Cate_Links;
            txtCateName.Value = category.Cate_Name;        

            chkStatus.Checked = category.Cate_Status;
        }
      //  btnAdd.Visible = false;
        btnEdit.Visible = true;
    }
    #endregion
    #region Insert/Update and upload image
    protected void Update()
    {
        string category_ID = Request.QueryString["Category_ID"];
        Category category = Category_Session.Get_Category_ByID(int.Parse(category_ID));
        if (category != null)
        {
            category.Cate_Links = txtCateLink.Value;
            category.Cate_Name = txtCateName.Value;
            category.Cate_ParentID = int.Parse(ddlCategory.SelectedValue);
            category.Cate_Status = chkStatus.Checked;
            Category_Session.Update_Category(category);
        }

    }
    protected void Insert()
    {
        Category category = new Category();
        //Gan du lieu vao doi tuong	
        category.Cate_Links = txtCateLink.Value;
        category.Cate_Name = txtCateName.Value;
        category.Cate_ParentID = int.Parse(ddlCategory.SelectedValue);
        category.Cate_Status = chkStatus.Checked;
        category.Cate_Image = "";
        //Them moi
        Category_Session.Insert_Category(category);
    }


    #endregion
    #region Button event

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Insert();
            WebMsgBox.Show("Thêm danh mục thành công.");

        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Thêm danh mục không thành công do đã có lỗi xảy ra.");
            throw ex;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Update();
            WebMsgBox.Show("Sửa danh mục thành công.");
        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Sửa danh mục không thành công do đã có lỗi xảy ra.");
            throw ex;
        }
    }
    #endregion
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageCategory.aspx");

    }
}
